#包含头文件
from xml.etree import ElementTree


#创建根元素
root = ElementTree.Element("AUTOSAR")

# 添加子元素及其属性
Child      = ElementTree.SubElement(root ,"Child",     attrib={"attr1": "Child"})
grandChild = ElementTree.SubElement(Child,"grandChild",attrib={"attr1": "grandChild"})
#grandChild = ElementTree.SubElement(Child,"grandChild",attrib={"attr1": "grandChild"})

# 添加文本内容
#Child.text      = "some txt"
grandChild.text = "some txt"

# 序列化为XML字符串（可选，如果只需要内存中的XML字符串）
xml_str = ElementTree.tostring(root, encoding="unicode", method="xml", xml_declaration=True)


# 或者直接写入文件（推荐，将XML保存到磁盘）
ElementTree.indent(root)
tree = ElementTree.ElementTree(root)
tree.write("FK.arxml", encoding="utf-8", xml_declaration=True, method="xml")